import 'package:get/get.dart';
import 'package:wan_android_flutter/common/base/base_controller.dart';
import 'package:wan_android_flutter/common/network/model/hot_web_model.dart';
import 'package:wan_android_flutter/common/network/model/hot_word_model.dart';

import '../../common/network/http_util.dart';
import '../../common/network/json_parse.dart';
import 'hot_state.dart';

class HotLogic extends BaseController {
  final HotState state = HotState();

  @override
  Future<void> onLoadData() async {
    super.onLoadData();
    loadHotWords();
    loadHotWebs();
  }

  Future<void> loadHotWords() async {
    final response = await httpUtil.get("/hotkey/json");
    if (response.statusCode == 200 && response.data is Map<String, dynamic>) {
      final hotWordModel = JsonParse.fromJsonMap<HotWordModel>(response.data);
      if (hotWordModel?.data?.isNotEmpty == true) {
        state.hotWords.assignAll(hotWordModel!.data!);
      }
    }
  }

  Future<void> loadHotWebs() async {
    final response = await httpUtil.get("/friend/json");
    if (response.statusCode == 200 && response.data is Map<String, dynamic>) {
      final hotWebModel = JsonParse.fromJsonMap<HotWebModel>(response.data);
      if (hotWebModel?.data?.isNotEmpty == true) {
        state.hotWebs.assignAll(hotWebModel!.data!);
      }
    }
  }
}
